package com.wxgo.config;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;
import com.wxgo.common.model._MappingKit;
import com.wxgo.controller.FileController;
import com.wxgo.controller.UploadController;
import com.wxgo.route.AdminRoute;
import com.wxgo.route.FrontRoute;

public class SysConfig extends JFinalConfig {

	private static Prop p = loadConfig();

	@Override
	public void configConstant(Constants me) {
		// 判断数据库备份路径文件是否存在 不存在新增
		// FileUtil.filePathExists(p.get("backDir"));
		me.setDevMode(p.getBoolean("devMode", false));
	}

	@Override
	public void configRoute(Routes me) {
		me.add(new AdminRoute());
		me.add(new FrontRoute());
		me.add("/upload", UploadController.class);
		me.add("/file", FileController.class);
		// me.add(new ForeRoute("com.moongo.controller.fore"));
	}

	@Override
	public void configEngine(Engine me) {
		me.addSharedFunction("/static/templets/css.jftl");
		me.addSharedFunction("/static/templets/js.jftl");
		me.addSharedFunction("/static/templets/paginate.jftl");
		me.addSharedFunction("/static/templets/header.jftl");
		me.addSharedFunction("/static/templets/footer.jftl");
	}

	@Override
	public void configPlugin(Plugins me) {
		// 配置Druid数据库连接池插件
		DruidPlugin druidPlugin = getDruidPlugin();
		WallFilter wallFilter = new WallFilter(); // 加强数据库安全
		wallFilter.setDbType("mysql");
		druidPlugin.addFilter(wallFilter);
		druidPlugin.addFilter(new StatFilter()); // 添加 StatFilter 才会有统计数据
		me.add(druidPlugin);

		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		me.add(arp);
		// 配置EhCache缓存插件
		// me.add(new EhCachePlugin());

		/*
		 * TIOPlugin tioPlugin = new TIOPlugin(); me.add(tioPlugin);
		 */

		// me.add(new Cron4jPlugin(p));

		// 所有配置在 MappingKit 中搞定
		_MappingKit.mapping(arp);
	}

	/**
	 * 抽取成独立的方法，例于 _Generator 中重用该方法，减少代码冗余
	 */
	public static DruidPlugin getDruidPlugin() {
		return new DruidPlugin(p.get("jdbcUrl"), p.get("user"), p.get("password").trim());
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new SessionInViewInterceptor());
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler("ctx"));
		// me.add(new TransHandler());
	}

	private static Prop loadConfig() {
		try {
			// 优先加载生产环境配置文件
			return PropKit.use("dbInfo.properties");
		} catch (Exception e) {
			// 找不到生产环境配置文件，再去找开发环境配置文件
			return PropKit.use("dbInfo_dev.properties");
		}
	}
}
